<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :md="6" :sm="8">
            <a-form-item :label="$t('m.bpm.entity.processDefName')">
              <a-input placeholder="" v-model="queryParam.processDefName"></a-input>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="8">
            <a-form-item :label="$t('m.bpm.entity.businessTitle')">
              <a-input placeholder="" v-model="queryParam.businessTitle"></a-input>
            </a-form-item>
          </a-col>
          <template v-if="toggleSearchStatus">
            <a-col :md="6" :sm="8">
              <a-form-item :label="$t('m.bpm.entity.processInstanceState')">
                <a-select v-model="queryParam.processInstanceState" placeholder="">
                  <a-select-option value="ALL">{{this.$t('m.common.label.all')}}</a-select-option>
                  <a-select-option v-for="item in processInstanceState" :key="item.value" :value="item.value">
                    {{item.text}}
                  </a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
          </template>
          <a-col :md="6" :sm="8">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">{{ $t('m.common.button.query') }}</a-button>
              <a-button type="default" @click="searchReset" icon="reload" style="margin-left: 8px">{{ $t('m.common.button.reset') }}</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? this.$t('m.common.button.fold') : this.$t('m.common.button.expand') }}
                <ych-icon :type="toggleSearchStatus ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="batchDel" type="primary"  icon="delete">{{ $t('m.common.button.delete') }}</a-button>
      <a-button @click="batchDelFilterFormData" type="default"  icon="delete">{{ $t('m.common.button.deleteFilterFormData') }}</a-button>
      <a-button @click="handoverWork" type="default" icon="solution">{{ $t('m.system.user.other.handoverWork') }}</a-button>
    </div>

    <!-- table区域-begin -->
    <div>
      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        @change="handleTableChange">
        <template slot="serialNum" slot-scope="text,record,index">
          {{(ipagination.current-1) * ipagination.pageSize + parseInt(index) + 1}}
        </template>
        <span slot="action" slot-scope="text, record">
          <a @click="handleDetail(record)">{{ $t('m.common.button.open') }}</a>
          <a-divider type="vertical"/>
          <a-dropdown>
            <a class="ant-dropdown-link">{{$t('m.common.button.more')}} <ych-icon type="down"/></a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a-popconfirm :title="$t('m.common.tip.delete_confirm')" @confirm="() => handleDelete(record.processInstanceId)">
                  <a>{{ $t('m.common.button.delete') }}</a>
                </a-popconfirm>
              </a-menu-item>
              <a-menu-item v-if="record.processInstanceState == 'ACTIVE'">
                <a-popconfirm :title="$t('m.bpm.verify.confirm_activate')" @confirm="() => handleSuspend(record)">
                  <a> {{$t('m.bpm.other.handleSuspend')}}</a>
                </a-popconfirm>
              </a-menu-item>
              <a-menu-item v-if="record.processInstanceState == 'SUSPENDED'">
                <a-popconfirm :title="$t('m.bpm.verify.confirm_suspend')" @confirm="() => handleActivate(record)">
                  <a> {{$t('m.bpm.other.handleActivate')}}</a>
                </a-popconfirm>
              </a-menu-item>
              <a-menu-item>
                  <a @click="showVariable(record)"> {{$t('m.bpm.other.showVariable')}}</a>
              </a-menu-item>
              <a-menu-item>
                  <a @click="bpmTrack(record)"> {{$t('m.bpm.other.bpmTrack')}}</a>
              </a-menu-item>
              <a-menu-item>
                  <a @click="showBpmTask(record)"> {{$t('m.bpm.other.showBpmTask')}}</a>
              </a-menu-item>
              <a-menu-item>
                  <a @click="showBpmTaskReader(record)"> {{$t('m.bpm.other.showBpmTaskReader')}}</a>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>

      </a-table>
    </div>

    <redirect-modal ref="flowForm" @refreshParent="modalFormOk"></redirect-modal>

    <!--流程跟踪对话框-->
    <bpm-track-dialog ref="bpmTrackDialog"></bpm-track-dialog>

    <!--流程变量对话框-->
    <bpm-variable-dialog ref="bpmVariableDialog" :title="currentInstance.businessTitle"
                         :process-instance-id="currentInstance.id"></bpm-variable-dialog>

    <!--流程任务项对话框-->
    <bpm-task-dialog ref="bpmTaskDialog" :title="currentInstance.businessTitle"
                     :process-instance-id="currentInstance.id"></bpm-task-dialog>

    <!--流程读者对话框-->
    <bpm-task-reader-dialog ref="bpmTaskReaderDialog" :title="currentInstance.businessTitle"
                     :process-instance-id="currentInstance.id"></bpm-task-reader-dialog>
    <bpm-handover ref="bpmHandover"></bpm-handover>
  </a-card>
</template>

<script>

  import { YcListMixin } from '@/mixins/YcListMixin'
  import { putAction } from '@/api/manage'
  import { filterMultiDictText } from '@/components/dict/YchDictSelectUtil'
  import RedirectModal from './redirect/RedirectModal'
  import BpmTrackDialog from './modules/BpmTrackDialog'
  import BpmVariableDialog from './modules/BpmVariableDialog'
  import BpmTaskDialog from './modules/BpmTaskDialog'
  import BpmTaskReaderDialog from './modules/BpmTaskReaderDialog'
  import { BpmConstant } from './BpmConstant'
  import YchConstant from "@/api/YchConstant";
  import BpmHandover from './modules/BpmHandover'

  export default {
    name: 'ProcessInstanceList',
    mixins: [YcListMixin],
    components: {
      RedirectModal,
      BpmTrackDialog,
      BpmVariableDialog,
      BpmTaskDialog,
      BpmTaskReaderDialog,
      BpmHandover
    },
    data () {
      return {
        processInstanceState: BpmConstant.processInstanceState,
        // 表头
        columns: [
          {
            title:this.$t('m.common.label.number'),
            dataIndex: '',
            key: 'rowIndex',
            width: "50px",
            align: 'center',
            scopedSlots: { customRender: 'serialNum' },
          },
          {
            title: this.$t('m.bpm.entity.processDefName'),
            align: 'center',
            width: "200px",
            dataIndex: 'processDefName'
          },
          {
            title: this.$t('m.bpm.entity.businessTitle'),
            align: 'left',
            dataIndex: 'businessTitle',
            ellipsis: true,
          },
          {
            title: this.$t('m.bpm.entity.processInstanceState'),
            align: 'center',
            dataIndex: 'processInstanceState',
            width: "120px",
            customRender: (text) => {
              if (!text) {
                return ''
              } else {
                return filterMultiDictText(BpmConstant.processInstanceState, text)
              }
            }
          },
          {
            title: this.$t('m.bpm.entity.startTimeString'),
            align: 'center',
            dataIndex: 'startTimeString',
            width: "130px"
          },
          {
            title: this.$t('m.bpm.entity.endTimeString'),
            align: 'center',
            dataIndex: 'endTimeString',
            width: "130px"
          },
          /*{
            title: this.$t('m.bpm.entity.durationString'),
            align: 'center',
            width: 70,
            dataIndex: 'duration'
          },*/
          {
            title: this.$t('m.bpm.entity.startUserName'),
            align: 'center',
            dataIndex: 'startUserName',
            width: "150px",
            customRender: function (t, r, index) {
              return t + (r.startDeptName ? '/' + r.startDeptName : '')
            }
          },
          {
            title: this.$t('m.common.label.operate'),
            dataIndex: 'action',
            align: 'center',
            scopedSlots: { customRender: 'action' },
            width: "120px"
          }
        ],
        url: {
          list: YchConstant.PREFIX_BPM + '/bpm-back/processInstanceRest/list/v1',
          delete: YchConstant.PREFIX_BPM + '/bpm-back/processInstanceRest/delete/v1',
          deleteBatch: YchConstant.PREFIX_BPM + '/bpm-back/processInstanceRest/deleteBatch/v1',
          deleteBatchFilterFormData: YchConstant.PREFIX_BPM + '/bpm-back/processInstanceRest/deleteBatchFilterFormData/v1',
          suspend: YchConstant.PREFIX_BPM + '/bpm-back/processInstanceRest/suspend/v1',
          activate: YchConstant.PREFIX_BPM + '/bpm-back/processInstanceRest/activate/v1'
        },
        currentInstance: {}
      }
    },
    inject: ['addRouter'],
    methods: {
      handoverWork(){
        this.$refs.bpmHandover.open()
      },
      handleDetail (record) {
        let router = null
        if (this.$store.getters.formOpenType === '2') {
          router = this.addRouter({
            path: '/bpm/detailFromManage/' + record.processInstanceId,
            component: resolve => require(['@/views/bpm/redirect/RedirectTab.vue'], resolve),
            props: {
              id: record.processInstanceId,
              func: 'detailFromManage',
            },
            meta: {
              title: this.$t('m.system.portlet.mytask.detailFromTask'),
            }
          });
          if (router) {
            this.$router.push(router).catch(err => {})
          }
        }
        if (!router) {
          this.$refs.flowForm.detailFromManage(record.processInstanceId)
        }
      },
      handleSuspend (record) {
        const that = this
        putAction(this.url.suspend + '?id=' + record.id, {}).then((res) => {
          if (res.success) {
            that.messageUtil.success(res.message)
            that.loadData()
          } else {
            that.messageUtil.warning(res.message)
          }
        }).finally(() => {
        })
      },
      handleActivate (record) {
        const that = this
        putAction(this.url.activate + '?id=' + record.id, {}).then((res) => {
          if (res.success) {
            that.messageUtil.success(res.message)
            that.loadData()
          } else {
            that.messageUtil.warning(res.message)
          }
        }).finally(() => {
        })
      },
      showVariable (record) {
        this.currentInstance = record
        this.$refs.bpmVariableDialog.visible = true
      },
      bpmTrack (record) {
        this.currentInstance = record
        this.$refs.bpmTrackDialog.open(record.id,record.processDefId)
      },
      showBpmTask (record) {
        this.currentInstance = record
        this.$refs.bpmTaskDialog.visible = true
      },
      showBpmTaskReader(record) {
        this.currentInstance = record
        this.$refs.bpmTaskReaderDialog.visible = true
      },
      batchDelFilterFormData() {
        this.batchDelByUrl(this.url.deleteBatchFilterFormData)
      }
    }
  }
</script>
<style lang="less" scoped>
</style>
